package com.baomidou.springwind.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.kisso.annotation.Permission;
import com.baomidou.springwind.entity.Others;
import com.baomidou.springwind.entity.Resource;
import com.baomidou.springwind.enums.ResourceEnum;
import com.baomidou.springwind.service.*;
import com.baomidou.springwind.utils.ParamMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.*;

/**
 * <p>
 * 仓储模拟相关操作
 * </p>
 *
 * @Author demo
 * @Date 2017-5-28
 */
@Controller
@RequestMapping("/view")
public class ViewController extends BaseController {

    @Autowired
    private ResourceService resourceService;

    @Autowired
    private OthersService othersService;

    @Autowired
    private ReceiveGoodsService receiveGoodsService;

    @Autowired
    private DeliveryGoodsService deliveryGoodsService;

    @Autowired
    private GoodsService goodsService;

    private DecimalFormat df = new DecimalFormat("##.##");

    @Permission("1001")
    @RequestMapping("/view1/{batchId}/{type}")
    public String list(@PathVariable String type, Model model) {
        model.addAttribute("batchId", getProjectId());
        return pjaxPath("/lpp/main" + type, model);
    }

    @ResponseBody
    @Permission("1001")
    @RequestMapping("/area/pie1")
    public String getAreaPieOne(Model model) {
        Long batchId=Long.valueOf(getProjectId());
        Resource resource = resourceService.getResource(batchId, ResourceEnum.WORKSPACE_STORE);
        Others others = othersService.getOthers(batchId);
        JSONArray array = new JSONArray();
        JSONObject json1 = new JSONObject();
        json1.put("name", "储区面积");
        json1.put("value", resource.getAmount());
        array.add(json1);
        JSONObject json2 = new JSONObject();
        json2.put("name", "物流中心建物面积");
        json2.put("value", others.getC18());
        array.add(json2);
        return callbackSuccess(array);
    }

    @ResponseBody
    @Permission("1001")
    @RequestMapping("/area/pie2")
    public String getAreaPieTwo( Model model) {
        Long batchId=Long.valueOf(getProjectId());
        Resource resource = resourceService.getResource(batchId, ResourceEnum.WORKSPACE_STORE);
        Others others = othersService.getOthers(batchId);
        JSONArray array = new JSONArray();
        JSONObject json1 = new JSONObject();
        json1.put("name", "储区面积");
        json1.put("value", resource.getAmount());
        array.add(json1);
        JSONObject json2 = new JSONObject();
        json2.put("name", "可保管面积");
        json2.put("value", others.getC16());
        array.add(json2);
        return callbackSuccess(array);
    }

    @ResponseBody
    @Permission("1001")
    @RequestMapping("/area/pie3")
    public String getAreaPieThree( Model model) {
        Long batchId=Long.valueOf(getProjectId());
        ParamMap<String, Object> pm=new ParamMap<String, Object>();
        pm.put("id",getCurrentUserId());
        pm.put("batchId",batchId);
        BigDecimal value = goodsService.selectStockVolume(pm);
        Others others = othersService.getOthers(batchId);
        JSONArray array = new JSONArray();
        JSONObject json1 = new JSONObject();
        json1.put("name", "储区面积");
        json1.put("value", value);
        array.add(json1);
        JSONObject json2 = new JSONObject();
        json2.put("name", "储位总容积");
        json2.put("value", others.getC17());
        array.add(json2);
        return callbackSuccess(array);
    }

    @ResponseBody
    @Permission("1001")
    @RequestMapping("/area/bar")
    public String getAreaBar( Model model) {
        Long batchId=Long.valueOf(getProjectId());
        List<Map<String, Object>> initialStockMapList = goodsService.selectGoodsInitialStock(batchId);
        Map<String, Integer> initialStockMap = new HashMap<String, Integer>();
        for (Map<String, Object> initialStock : initialStockMapList) {
            initialStockMap.put(initialStock.get("code").toString(), (Integer) initialStock.get("initial_stock"));
        }
        List<Map<String, Object>> timeStockMapList = goodsService.selectGoodsTimeStock(batchId);
        Integer initialStock = 0;
        JSONObject ret = new JSONObject();
        JSONArray name = new JSONArray();
        JSONArray value = new JSONArray();
        Map<String, Integer> plCount = new LinkedHashMap<String, Integer>();
        for (Map<String, Object> timeStock : timeStockMapList) {
            initialStock = initialStockMap.get(timeStock.get("code"));
            if (initialStock == null) {
                continue;
            }
            if (initialStock + ((BigDecimal) timeStock.get("amount")).intValue() > 0) {
                Integer count = plCount.get(timeStock.get("time"));
                plCount.put(timeStock.get("time").toString(), count == null ? 1 : count + 1);
            }
        }
        Others others = othersService.getOthers(batchId);
        for (Map.Entry<String, Integer> entry : plCount.entrySet()) {
            name.add(entry.getKey());
            value.add(df.format(others.getC15().floatValue() / entry.getValue()));
        }
        ret.put("name", name);
        ret.put("value", value);
        return callbackSuccess(ret);
    }

    @ResponseBody
    @Permission("1001")
    @RequestMapping("/cost/pie1")
    public String getCostPieOne( Model model) {
        Long batchId=Long.valueOf(getProjectId());
        Resource unloader = resourceService.getResource(batchId, ResourceEnum.OPERATOR_UNLOADER);
        Resource store = resourceService.getResource(batchId, ResourceEnum.OPERATOR_STORE);
        Resource out = resourceService.getResource(batchId, ResourceEnum.OPERATOR_OUT);
        Resource dispatch = resourceService.getResource(batchId, ResourceEnum.OPERATOR_DISPATCH);
        Resource admin = resourceService.getResource(batchId, ResourceEnum.OPERATOR_ADMIN);

        List<PieData> list = new ArrayList<PieData>(4);
//        BigDecimal amountAll = unloader.getAmount().add(store.getAmount());
//        BigDecimal price = unloader.getPrice().add(store.getPrice()).divide(new BigDecimal("2"));
        list.add(new PieData("入库管理班组", df.format(unloader.getPrice().multiply(unloader.getAmount()).add(store.getPrice().multiply(store.getAmount())))));
        list.add(new PieData("出库管理班组", df.format(out.getPrice().multiply(out.getAmount()))));
        list.add(new PieData("配送管理班组", df.format(dispatch.getPrice().multiply(dispatch.getAmount()))));
        list.add(new PieData("其他管理人员", df.format(admin.getPrice().multiply(admin.getAmount()))));
        return callbackSuccess(list);
    }

    @ResponseBody
    @Permission("1001")
    @RequestMapping("/cost/pie2")
    public String getCostPieTwo( Model model) {
        Long batchId=Long.valueOf(getProjectId());
        Resource store = resourceService.getResource(batchId, ResourceEnum.EQUIPMENT_STORE);
        Resource hand = resourceService.getResource(batchId, ResourceEnum.EQUIPMENT_HAND_TRUCK);
        Resource electric = resourceService.getResource(batchId, ResourceEnum.EQUIPMENT_ELECTRIC_TRUCK);
        Resource pickup = resourceService.getResource(batchId, ResourceEnum.EQUIPMENT_PICKUP_TRUCK);
        Resource salver = resourceService.getResource(batchId, ResourceEnum.EQUIPMENT_SALVER);
        Resource other = resourceService.getResource(batchId, ResourceEnum.EQUIPMENT_OTHER_CASE);

        List<PieData> list = new ArrayList<PieData>(4);
        list.add(new PieData("存储设备", df.format(store.getPrice().multiply(store.getAmount()))));
        list.add(new PieData("搬运设备", df.format(hand.getPrice().multiply(hand.getAmount()).add(electric.getPrice().multiply(electric.getAmount())))));
        list.add(new PieData("分拣设备", df.format(pickup.getPrice().multiply(pickup.getAmount()))));
        list.add(new PieData("物流容器", df.format(salver.getPrice().multiply(salver.getAmount()).add(other.getPrice().multiply(other.getAmount())))));
        return callbackSuccess(list);
    }

    @ResponseBody
    @Permission("1001")
    @RequestMapping("/cost/pie3")
    public String getCostPieThree( Model model) {
        Long batchId=Long.valueOf(getProjectId());
        Resource unload = resourceService.getResource(batchId, ResourceEnum.WORKSPACE_UNLOAD);
        Resource inspect = resourceService.getResource(batchId, ResourceEnum.WORKSPACE_INSPECT);
        Resource store = resourceService.getResource(batchId, ResourceEnum.WORKSPACE_STORE);
        Resource pickup = resourceService.getResource(batchId, ResourceEnum.WORKSPACE_PICKUP);
        Resource pack = resourceService.getResource(batchId, ResourceEnum.WORKSPACE_PACKAGE);
        Resource recheck = resourceService.getResource(batchId, ResourceEnum.WORKSPACE_RECHECK);
        Resource out = resourceService.getResource(batchId, ResourceEnum.WORKSPACE_OUT);
        Resource office = resourceService.getResource(batchId, ResourceEnum.WORKSPACE_OFFICE);

        List<PieData> list = new ArrayList<PieData>(4);
        list.add(new PieData("入库区域设施", df.format(unload.getPrice().multiply(unload.getAmount()))));
        list.add(new PieData("存储区域设施", df.format(store.getPrice().multiply(store.getAmount()))));
        list.add(new PieData("出库区域设施", df.format(out.getPrice().multiply(out.getAmount()))));
        BigDecimal other = inspect.getPrice().multiply(inspect.getAmount())
                .add(pickup.getPrice().multiply(pickup.getAmount()))
                .add(pack.getPrice().multiply(pack.getAmount()))
                .add(recheck.getPrice().multiply(recheck.getAmount()))
                .add(office.getPrice().multiply(office.getAmount()));
        list.add(new PieData("其他区域设施", df.format(other)));
        return callbackSuccess(list);
    }

    @ResponseBody
    @Permission("1001")
    @RequestMapping("/inout/bar1")
    public String getInoutBarOne(Model model) {
        Long batchId=Long.valueOf(getProjectId());
        List<Map<String, Object>> mapList = receiveGoodsService.selecthourAmount(batchId);
        Resource unload = resourceService.getResource(batchId, ResourceEnum.WORKSPACE_UNLOAD);
        Resource store = resourceService.getResource(batchId, ResourceEnum.WORKSPACE_STORE);
        int count = unload.getAmount().add(store.getAmount()).intValue();
        JSONObject ret = new JSONObject();
        JSONArray name = new JSONArray();
        JSONArray value = new JSONArray();
        for (Map<String, Object> map : mapList) {
            name.add(map.get("times"));
            value.add(((BigDecimal) map.get("amount")).intValue() / ((Long) map.get("hours") + count));
        }
        ret.put("name", name);
        ret.put("value", value);
        return callbackSuccess(ret);
    }

    @ResponseBody
    @Permission("1001")
    @RequestMapping("/inout/bar2")
    public String getInoutBarTwo( Model model) {
        Long batchId=Long.valueOf(getProjectId());
        List<Map<String, Object>> mapList = deliveryGoodsService.selecthourAmount(batchId);
        Resource out = resourceService.getResource(batchId, ResourceEnum.WORKSPACE_OUT);
        int count = out.getAmount().intValue();
        JSONObject ret = new JSONObject();
        JSONArray name = new JSONArray();
        JSONArray value = new JSONArray();
        for (Map<String, Object> map : mapList) {
            name.add(map.get("times"));
            value.add(((BigDecimal) map.get("amount")).intValue() / ((Long) map.get("hours") + count));
        }
        ret.put("name", name);
        ret.put("value", value);
        return callbackSuccess(ret);
    }

    @ResponseBody
    @Permission("1001")
    @RequestMapping("/inout/line1")
    public String getInoutLineOne( Model model) {
        Long batchId=Long.valueOf(getProjectId());
        List<Map<String, Object>> mapList = receiveGoodsService.selecthourAmount(batchId);
        Others others = othersService.getOthers(batchId);
        JSONObject ret = new JSONObject();
        JSONArray name = new JSONArray();
        JSONArray value = new JSONArray();
        for (Map<String, Object> map : mapList) {
            name.add(map.get("times"));
            value.add(df.format((Long) map.get("hours") * 100 / others.getC6()));
        }
        ret.put("name", name);
        ret.put("value", value);
        return callbackSuccess(ret);
    }

    @ResponseBody
    @Permission("1001")
    @RequestMapping("/inout/line2")
    public String getInoutLineTwo( Model model) {
        Long batchId=Long.valueOf(getProjectId());
        List<Map<String, Object>> mapList = deliveryGoodsService.selecthourAmount(batchId);
        Others others = othersService.getOthers(batchId);
        JSONObject ret = new JSONObject();
        JSONArray name = new JSONArray();
        JSONArray value = new JSONArray();
        for (Map<String, Object> map : mapList) {
            name.add(map.get("times"));
            value.add(df.format((Long) map.get("hours") * 100 / others.getC6()));
        }
        ret.put("name", name);
        ret.put("value", value);
        return callbackSuccess(ret);
    }

    @ResponseBody
    @Permission("1001")
    @RequestMapping("/inout/bar3")
    public String getInoutBarThree( Model model) {
        Long batchId=Long.valueOf(getProjectId());
        List<Map<String, Object>> mapList = goodsService.selectGoodsDayAmount(batchId);
        Resource unloader = resourceService.getResource(batchId, ResourceEnum.OPERATOR_UNLOADER);
        Resource store = resourceService.getResource(batchId, ResourceEnum.OPERATOR_STORE);
        Resource out = resourceService.getResource(batchId, ResourceEnum.OPERATOR_OUT);
        int count = unloader.getAmount().add(store.getAmount()).add(out.getAmount()).intValue();
        JSONObject ret = new JSONObject();
        JSONArray name = new JSONArray();
        JSONArray value = new JSONArray();
        for (Map<String, Object> map : mapList) {
            name.add(map.get("time"));
            value.add(df.format(((BigDecimal) map.get("amount")).floatValue() / ((BigDecimal) map.get("hours")).floatValue() / count));
        }
        ret.put("name", name);
        ret.put("value", value);
        return callbackSuccess(ret);
    }

    @ResponseBody
    @Permission("1001")
    @RequestMapping("/inout/line3")
    public String getInoutLineThree( Model model) {
        Long batchId=Long.valueOf(getProjectId());
        List<Map<String, Object>> mapList = goodsService.selectGoodsDayAmount(batchId);
        Others others = othersService.getOthers(batchId);
        JSONObject ret = new JSONObject();
        JSONArray name = new JSONArray();
        JSONArray value = new JSONArray();
        for (Map<String, Object> map : mapList) {
            name.add(map.get("time"));
            value.add(df.format(((BigDecimal) map.get("hours")).floatValue() * 100 / others.getC6()));
        }
        ret.put("name", name);
        ret.put("value", value);
        return callbackSuccess(ret);
    }

    @ResponseBody
    @Permission("1001")
    @RequestMapping("/inout/bar4")
    public String getInoutLineFour( Model model) {
        Long batchId= Long.valueOf(getProjectId());
        List<Map<String, Object>> mapList = goodsService.selectGoodsDayAmount(batchId);
        Resource electric = resourceService.getResource(batchId, ResourceEnum.EQUIPMENT_ELECTRIC_TRUCK);
        Resource hand = resourceService.getResource(batchId, ResourceEnum.EQUIPMENT_HAND_TRUCK);
        int count = electric.getAmount().add(hand.getAmount()).intValue();
        JSONObject ret = new JSONObject();
        JSONArray name = new JSONArray();
        JSONArray value = new JSONArray();
        for (Map<String, Object> map : mapList) {
            name.add(map.get("time"));
            value.add(df.format(((BigDecimal) map.get("amount")).floatValue() / count));
        }
        ret.put("name", name);
        ret.put("value", value);
        return callbackSuccess(ret);
    }

    @ResponseBody
    @Permission("1001")
    @RequestMapping("/inout/bar5")
    public String getInoutLineFive( Model model) {
        Long batchId=Long.valueOf(getProjectId());
        List<Map<String, Object>> mapList = goodsService.selectGoodsDayAmount(batchId);
        Resource electric = resourceService.getResource(batchId, ResourceEnum.EQUIPMENT_ELECTRIC_TRUCK);
        Resource hand = resourceService.getResource(batchId, ResourceEnum.EQUIPMENT_HAND_TRUCK);
        int count = electric.getAmount().add(hand.getAmount()).intValue();
        JSONObject ret = new JSONObject();
        JSONArray name = new JSONArray();
        JSONArray value = new JSONArray();
        for (Map<String, Object> map : mapList) {
            name.add(map.get("time"));
            value.add(df.format(((BigDecimal) map.get("amount")).floatValue() / ((BigDecimal) map.get("hours")).floatValue() / count));
        }
        ret.put("name", name);
        ret.put("value", value);
        return callbackSuccess(ret);
    }

    @ResponseBody
    @Permission("1001")
    @RequestMapping("/nonjob/bar1")
    public String getNonjobBarOne( Model model) {
        Long batchId=Long.valueOf(getProjectId());
        List<Map<String, Object>> mapList = goodsService.selectGoodsMoney(batchId);
        Others others = othersService.getOthers(batchId);
        JSONObject ret = new JSONObject();
        JSONArray name = new JSONArray();
        JSONArray value = new JSONArray();
        for (Map<String, Object> map : mapList) {
            name.add(map.get("time"));
            value.add(df.format(((BigDecimal) map.get("money")).floatValue() / others.getC18().floatValue()));
        }
        ret.put("name", name);
        ret.put("value", value);
        return callbackSuccess(ret);
    }

    @ResponseBody
    @Permission("1001")
    @RequestMapping("/nonjob/bar2")
    public String getNonjobBarTwo( Model model) {
        Long batchId=Long.valueOf(getProjectId());
        List<Map<String, Object>> mapList = deliveryGoodsService.selecthourAmount(batchId);
        Long count = resourceService.selectCompanyPersonCount(batchId);
        JSONObject ret = new JSONObject();
        JSONArray name = new JSONArray();
        JSONArray value = new JSONArray();
        for (Map<String, Object> map : mapList) {
            name.add(map.get("times"));
            value.add(df.format(((BigDecimal) map.get("amount")).floatValue() / count));
        }
        ret.put("name", name);
        ret.put("value", value);
        return callbackSuccess(ret);
    }

    @ResponseBody
    @Permission("1001")
    @RequestMapping("/nonjob/bar3")
    public String getNonjobBarThree(Model model) {
        Long batchId=Long.valueOf(getProjectId());
        List<Map<String, Object>> mapList = goodsService.selectGoodsMoney(batchId);
        Long count = resourceService.selectCompanyPersonCount(batchId);
        JSONObject ret = new JSONObject();
        JSONArray name = new JSONArray();
        JSONArray value = new JSONArray();
        for (Map<String, Object> map : mapList) {
            name.add(map.get("time"));
            value.add(df.format(((BigDecimal) map.get("money")).floatValue() / count));
        }
        ret.put("name", name);
        ret.put("value", value);
        return callbackSuccess(ret);
    }

    @ResponseBody
    @Permission("1001")
    @RequestMapping("/nonjob/pie1")
    public String getNonjobPieOne( Model model) {
        Long batchId=Long.valueOf(getProjectId());
        Long count = resourceService.selectCompanyPersonCount(batchId);
        Resource admin = resourceService.getResource(batchId, ResourceEnum.OPERATOR_ADMIN);
        JSONArray array = new JSONArray();
        JSONObject json1 = new JSONObject();
        json1.put("name", "作业员数");
        json1.put("value", count - admin.getAmount().intValue());
        array.add(json1);
        JSONObject json2 = new JSONObject();
        json2.put("name", "间接作业员数");
        json2.put("value", admin.getAmount().intValue());
        array.add(json2);
        return callbackSuccess(array);
    }

    @ResponseBody
    @Permission("1001")
    @RequestMapping("/nonjob/line1")
    public String getNonjobLine1( Model model) {
        Long batchId=Long.valueOf(getProjectId());
        List<Map<String, Object>> mapList = goodsService.selectGoodsMoney(batchId);
        Others others = othersService.getOthers(batchId);
        JSONObject ret = new JSONObject();
        JSONArray name = new JSONArray();
        JSONArray value = new JSONArray();
        for (Map<String, Object> map : mapList) {
            name.add(map.get("time"));
            value.add(df.format(((BigDecimal) map.get("money")).floatValue() / others.getC2().floatValue()));
        }
        ret.put("name", name);
        ret.put("value", value);
        return callbackSuccess(ret);
    }

    @ResponseBody
    @Permission("1001")
    @RequestMapping("/nonjob/gauge")
    public String getNonjobGauge( Model model) {
        Long batchId=Long.valueOf(getProjectId());
        Others others = othersService.getOthers(batchId);
        Long count = resourceService.selectCompanyPersonCount(batchId);
        List<PieData> list = new ArrayList<PieData>(4);
//        BigDecimal amountAll = unloader.getAmount().add(store.getAmount());
//        BigDecimal price = unloader.getPrice().add(store.getPrice()).divide(new BigDecimal("2"));
        list.add(new PieData("劳动装备率", df.format(others.getC2().floatValue() / count)));
        return callbackSuccess(list);
    }
}
